<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    switchFullscreen
    @ok="saveDraft"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel">
    <inventory-count-form ref="realForm" @ok="submitCallback" :disabled="disableSubmit" />
    <template #footer>
      <a-button key="back" @click="handleCancel">取消</a-button>
      <a-button v-if="!disableSubmit" type="primary" @click="saveDraft">暂存</a-button>
      <a-button v-if="!disableSubmit" type="primary" @click="handleSubmit">提交</a-button>
    </template>
  </j-modal>
</template>

<script>

import InventoryCountForm from './InventoryCountForm'

export default {
  name: 'InventoryCountModal',
  components: {
    InventoryCountForm
  },
  data() {
    return {
      title: '',
      width: 800,
      visible: false,
      disableSubmit: false
    }
  },
  methods: {
    add() {
      this.visible = true
    },
    edit(record) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.realForm.edit(record)
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    saveDraft(e) {
      this.handleButtonState(e.target)
      this.$refs.realForm.saveDraft()
    },
    handleSubmit(e) {
      this.handleButtonState(e.target)
      this.$refs.realForm.handleSubmit()
    },
    handleButtonState(button) {
      button.setAttribute('disabled', 'true')
      button.innerHTML = '处理中'
      setTimeout(() => {
        button.removeAttribute('disabled')
        button.innerHTML = '确定'
      }, 2000)
    },
    submitCallback() {
      this.$emit('ok')
      this.visible = false
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>

<style scoped>
</style>